package com.helei.config;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.helei.entity.root.MessageGroup;
import com.helei.strategy.StrategyContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.websocket.OnMessage;
import javax.websocket.server.ServerEndpoint;
import java.util.Objects;

@Slf4j
@ServerEndpoint("/webSocket")
@Component
public class WebSocketServer {
    private static StrategyContext strategyContext;
    @Autowired
    private void setStringRedisTemplate(StrategyContext strategyContext) {
        WebSocketServer.strategyContext = strategyContext;
    }
    //收到客户端信息
    @OnMessage
    public void onMessage(String message) {
        MessageGroup messageGroup = new Gson().fromJson(message, new TypeToken<MessageGroup>() {
        }.getType());
        if (Objects.equals(messageGroup.getPostType(),"meta_event")||Objects.equals(messageGroup.getPostType(),"notice")) {
            return;
        }
        strategyContext.calcPrice(messageGroup);
    }

}
